﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;
using NHibernate.Criterion;

namespace RMD.Modal
{
    public class DBDepartmentRepository : IDepartmentRepository
    {
        #region IDepartmentRepository Members

        public IList<Department> GetDepartments()
        {
            ISession session = NHHelper.GetNHSession();
            ICriteria crit = session.CreateCriteria<Department>();
            return crit.List<Department>();
        }

        public void AddDepartment(Department department)
        {
            ISession session = NHHelper.GetNHSession();
            ITransaction tx = session.BeginTransaction();
            session.Save(department);
            //session.Flush();
            tx.Commit();
            session.Close();
        }

        public IList<Department> SearchDepartmentByName(string name)
        {
            ISession session = NHHelper.GetNHSession();
            ICriteria crit = session.CreateCriteria<Department>();
            crit.Add(Expression.Like("Name", name, MatchMode.Anywhere));
            return crit.List<Department>();
        }

        public IList<Department> SearchDepartmentByNumber(string number)
        {
            ISession session = NHHelper.GetNHSession();
            ICriteria crit = session.CreateCriteria<Department>();
            crit.Add(Expression.Like("Number", number, MatchMode.Start));
            return crit.List<Department>();
        }
        #endregion
    }
}
